/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;


/**
 *
 * @author Carlos
 */
public class AnadirAmigo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
       if (request.getParameter("Salir")!=null){
            RequestDispatcher dispatcher = request.getRequestDispatcher("mostrarUsuario");
            dispatcher.forward (request, response); 
            return;
       }
        String email2= request.getParameter ("Usuario");
        if("".equals(email2)||email2==null){
             notificarError (request, response, "Error", "No se ha seleccionado ningún usuario");
             return;
        }
        
       SocialFP red= new SocialFP();
        /*try {
                    red.recuperaUjaenSocial();
                }catch (Exception e){
                 notificarError (request, response, "Error", "No se puede recuperar el estado de la red");
                }
        
        if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }*/
       red =  (SocialFP) request.getSession ().getAttribute ("red");
        String email=  (String) request.getSession ().getAttribute ("email");
         String clave=  (String) request.getSession ().getAttribute ("clave");
        Usuario u = red.loginUsuario(email, clave);
        if (red == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
         if (u == null) {
            notificarError (request, response, "Error", "Acceso ilegal o sesión caducada");
            return;
        }
        
        Usuario temp= red.buscarUsuarioEmail(email2);
        try{
            temp.solicitudAmistad(u);
            notificarExito (request, response, "Realizado", "Has mandado una solicitud a: " + temp.leeNombre());
            return;
        }catch (Exception e){
             notificarError (request, response, "Error", "No se puede mandar una solicitud a ese usuario");
            return;
        }
             
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    protected void notificarError (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }  
    protected void notificarExito (HttpServletRequest request, HttpServletResponse response, String cabecera, String descripcion) 
        throws ServletException, IOException
    {
        RequestDispatcher dispatcher = request.getRequestDispatcher("realizado.jsp?cabecera=" + cabecera 
                + "&descripcion=" + descripcion + "&continuar=mostrarUsuario");
        dispatcher.forward (request, response);    
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
